package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.Vendor;

public class VendorPresenter extends AbstractPresenter<Vendor> {
	final String SINGLEITEM = "vendors";
	final String MULTIITEM = "vendorss";
	
	public ArrayList<Vendor> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Vendor tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Vendor tryJSONParseNoHeader(String input){
		try{
			JSONObject jsonHop = new JSONObject(input);
			Vendor vendor = createItemFromJSONObject(jsonHop);
			return vendor;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Vendor createItemFromJSONObject(JSONObject jsonVendor){
		Vendor vend = new Vendor();
		try{
			vend.setID(jsonVendor.getInt("vid"));
			vend.setVendorAddress(jsonVendor.getString("VAddr"));
			vend.setVendorCity(jsonVendor.getString("VCity"));
			vend.setVendorEmail(jsonVendor.getString("VEmail"));
			vend.setVendorEmailCC(jsonVendor.getString("VEmailCC"));
			vend.setVendorName(jsonVendor.getString("VName"));
			vend.setVendorNotes(jsonVendor.getString("VNotes"));
			vend.setVendorPhone(jsonVendor.getString("VPhone"));
			vend.setVendorState(jsonVendor.getString("VState"));
			vend.setVendorZipCode(jsonVendor.getString("VZip"));
			return vend;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Vendor item){
		try{
			JSONObject json = new JSONObject();
			json.put("vid", item.getID());
			json.put("VAddr", item.getVendorAddress());
			json.put("VCity", item.getVendorCity());
			json.put("VEmail", item.getVendorEmail());
			json.put("VEmailCC", item.getVendorEmailCC());
			json.put("VName", item.getVendorName());
			json.put("VNotes", item.getVendorNotes());
			json.put("VPhone", item.getVendorPhone());
			json.put("VState", item.getVendorState());
			json.put("VZip", item.getVendorZipCode());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Vendor getEmptyEntity(){
		Vendor entity = new Vendor();
		return entity;
	}
}
